package com.ztesoft.sca.controller;

import com.ztesoft.sca.common.Constants;
import com.ztesoft.sca.model.ImAreaDto;
import com.ztesoft.sca.model.user.ImOrganizationPo;
import com.ztesoft.sca.service.ImAreaService;
import com.ztesoft.sca.util.exception.ExceptionUtil;
import com.ztesoft.sca.util.message.ResponseUtils;
import com.ztesoft.sca.util.seq.SequenceCreator;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/*
 * hxz 区域操作类
 * */

@RestController
public class ImAreaController {
	private static final Logger logger = Logger.getLogger(ImAreaController.class);
    
	@Autowired
    private ImAreaService iaService;
	
    /*
     * hxz 区域查询
     * */
	@RequestMapping(value = "/imarea/list",method = {RequestMethod.GET})
	public String imAreaList(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------imarea--list-----");
		JSONObject qryJson = JSONObject.fromObject(requestBody);
		int from=qryJson.getInt("page");//第1页开始
		int size=qryJson.getInt("rownum"); //每页大小
		
		Long areaId=qryJson.getLong("areaId");
		Long parentId=qryJson.getLong("parentId");
		String areaCode=qryJson.getString("areaCode");
		String areaName=qryJson.getString("areaName");
		String code=qryJson.getString("code");
		String c3Code=qryJson.getString("c3Code");
		
		ImAreaDto iaDto=new ImAreaDto();
		iaDto.setAreaId(areaId);
		iaDto.setParentId(parentId);
		iaDto.setAreaCode(areaCode);
		iaDto.setAreaName(areaName);
		iaDto.setCode(code);
		iaDto.setC3Code(c3Code);
		
		List<ImAreaDto> list = new ArrayList<>();
		try {
			list = this.iaService.listImArea(iaDto, from, size);
		} catch (Exception e) {
			 logger.error(" ImAreaList occur an error!",e);
			 return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
		}
        JSONArray json = new JSONArray();
        for(ImAreaDto pLog : list){
            JSONObject jo = new JSONObject();
            jo.put("areaId", pLog.getAreaId());
            jo.put("parentId", pLog.getParentId());
            jo.put("areaCode", pLog.getAreaCode());
            jo.put("areaName", pLog.getAreaName());
            jo.put("code", pLog.getCode());
            jo.put("c3Code", pLog.getC3Code());
            
            json.add(jo);
        }
		JSONObject jsonStrAll = new JSONObject();
		int records=list.size();
		int total=records/size;
		//int page=records/size;
		jsonStrAll.put("rows", json);
		jsonStrAll.put("page", from);
		//jsonStrAll.put("total", total);
		jsonStrAll.put("records", records);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,jsonStrAll);	
	}
	
	
	/*
     * hxz 查询区域下组织
     * */
	@RequestMapping(value = "/imarea/listOrg",method = {RequestMethod.GET})
	public String imAreaListOrg(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------imarea--listOrg-----");
		JSONObject qryJson = JSONObject.fromObject(requestBody);
		int from=qryJson.getInt("page");//第1页开始
		int size=qryJson.getInt("rownum"); //每页大小
		
		Long areaId=qryJson.getLong("areaId");
		Long parentId=qryJson.getLong("parentId");
		String areaCode=qryJson.getString("areaCode");
		String areaName=qryJson.getString("areaName");
		String code=qryJson.getString("code");
		String c3Code=qryJson.getString("c3Code");
		
		ImAreaDto iaDto=new ImAreaDto();
		iaDto.setAreaId(areaId);
		iaDto.setParentId(parentId);
		iaDto.setAreaCode(areaCode);
		iaDto.setAreaName(areaName);
		iaDto.setCode(code);
		iaDto.setC3Code(c3Code);
		
		List<ImOrganizationPo> list = new ArrayList<>();
		try {
			list = this.iaService.listImAreaOrg(iaDto, from, size);
		} catch (Exception e) {
			 logger.error(" ImAreaListOrg occur an error!",e);
			 return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
		}
        JSONArray json = new JSONArray();
        for(ImOrganizationPo pLog : list){
        	JSONObject jo = new JSONObject();
            jo.put("orgId", pLog.getOrgId());
            jo.put("areaId", pLog.getAreaId());
            jo.put("orgCode", pLog.getOrgCode());
            jo.put("orgName", pLog.getOrgName());
            jo.put("parentId", pLog.getParentId());
            jo.put("orgCodePath", pLog.getOrgCodePath());
            jo.put("orgNamePath", pLog.getOrgNamePath());
            jo.put("orgAlias", pLog.getOrgAlias());
            
            json.add(jo);
        }
		JSONObject jsonStrAll = new JSONObject();
		int records=list.size();
		int total=records/size;
		//int page=records/size;
		jsonStrAll.put("rows", json);
		jsonStrAll.put("page", from);
		//jsonStrAll.put("total", total);
		jsonStrAll.put("records", records);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,jsonStrAll);	
	}
	
	/*
     * hxz 区域查询TREE
     * */
	@RequestMapping(value = "/imarea/listTree",method = {RequestMethod.GET})
	public String imAreaListTree(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------imarea--listTree-----");
		JSONObject qryJson = JSONObject.fromObject(requestBody);
		
		Long parentId=qryJson.getLong("parentId");
		
		ImAreaDto iaDto=new ImAreaDto();
		iaDto.setParentId(parentId);
		
		List<ImAreaDto> list = new ArrayList<>();
		try {
			list = this.iaService.listImAreaTree(iaDto);
		} catch (Exception e) {
			 logger.error(" ImAreaListTree occur an error!",e);
			 return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
		}
        
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,list);	
	}
	
	
	/*
	 * hxz 区域新增
	 * */
	@RequestMapping(value = "/imarea/add",method = {RequestMethod.POST})
	public String imAreaAdd(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------imarea--add-----");
        JSONObject json = JSONObject.fromObject(requestBody);
        long areaId = 0L;
        try {
        	areaId = SequenceCreator.getSequence("IM_AREA_SEQ");
        } catch (Exception e) {
           logger.error("sequenceCreator obtain IM_AREA occur an error!",e);
           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
        }
        
		Long parentId=json.getLong("parentId");
		String areaCode=json.getString("areaCode");
		String areaName=json.getString("areaName");
		String code=json.getString("code");
		String c3Code=json.getString("c3Code");
		
		ImAreaDto iaDto=new ImAreaDto();
		iaDto.setAreaId(areaId);
		iaDto.setParentId(parentId);
		iaDto.setAreaCode(areaCode);
		iaDto.setAreaName(areaName);
		iaDto.setCode(code);
		iaDto.setC3Code(c3Code);
       
       try {
           this.iaService.insertImArea(iaDto);
       } catch (Exception e) {
           logger.error("create IM_AREA occur an error !",e);
           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
       }
       return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,iaDto);
	}
		
	/*
	 * hxz 区域修改
	 * */
	@RequestMapping(value = "/imarea/modify",method = {RequestMethod.POST})
	public String imAreaModify(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------imarea--modify-----");
        JSONObject json = JSONObject.fromObject(requestBody);
        
        Long areaId=json.getLong("areaId");
        Long parentId=json.getLong("parentId");
		String areaCode=json.getString("areaCode");
		String areaName=json.getString("areaName");
		String code=json.getString("code");
		String c3Code=json.getString("c3Code");
       
       
		ImAreaDto iaDto=new ImAreaDto();
		iaDto.setAreaId(areaId);
		iaDto.setParentId(parentId);
		iaDto.setAreaCode(areaCode);
		iaDto.setAreaName(areaName);
		iaDto.setCode(code);
		iaDto.setC3Code(c3Code);
       
       try {
           this.iaService.modifyImArea(iaDto);
       } catch (Exception e) {
           logger.error("modify IM_AREA occur an error !",e);
           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
       }
       return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,iaDto);
	}
	
	/*
	 * hxz 区域删除
	 * */
	@RequestMapping(value = "/imarea/del",method = {RequestMethod.POST})
	public String imAreaDel(@RequestBody String requestBody,HttpServletRequest request) {
		logger.info("-------imarea--del-----");
        JSONObject json = JSONObject.fromObject(requestBody);
       
        Long areaId=json.getLong("areaId");
       
  		ImAreaDto iaDto=new ImAreaDto();
		iaDto.setAreaId(areaId);        
       
       try {
           this.iaService.delImArea(iaDto);
       } catch (Exception e) {
           logger.error("del IM_BUTTON occur an error !",e);
           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
       }
       return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,iaDto);
	}
}
